|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
C++ in Plain English
The size of a data type determines its range. For example, the range of a short is -32,768 to 32,767. The range of a long is approximately plus or minus two billion. For a more comprehensive list of data types and their ranges, see the topic Data Types in Part III. More About #includeIf youre like me, you dont like to place gobbledygook into your program without knowing what its all about. I can hear you saying, Why do I have to enter this #include thing? Shouldnt I just be able to tell C++ Print the darn string!? I sympathize, particularly if youre coming from Basic and all youve had to do is to say PRINT X. However, the #include directive serves a very good purpose. In C++, every function (except main) must be declared before being used. This goes for standard library functions, such as printf. Rather than declare printf yourself, it is easier to include a special file for this purpose (called a header file) that includes all the declarations you need. For example, the file STDIO.H has definitions for all the standard input/output functions, such as printf. There are a couple of catches with #include. First, do not place a semicolon at the end. This rule seems a little arbitrary, although there are technical reasons for it. In any case, just commit to memory this Golden Rule of Directives: If a keyword begins with a pound sign (#), it is a directive, which means that you dont end the line with a semicolon (;). The second catch is that you have to know which header file to refer to. The solution is easy: when you look up the function in standard-library documentation, the first thing it tells you is which header file to use. You never include the same header file more than once. If you call 10 functions but they are all declared in STDIO.H, for example, you need only one #include directive. Once youre familiar with the C++ standard library, you can usually figure out which header file(s) you need without having to look up anything. The most common library functions fall into one of the following general categories shown in Table 2.2.
The following syntax display summarizes the general pattern for a simple C program. It doesnt involve any functions except main, but later in the chapter later, well add functions to the syntax. Note that all the #include directives should come before anything else in the program.
include_directives
void main () {
data_declarations_and_other_statements
}
The placeholder data_declarations_and_other_statements includes both data declarations, which you were introduced to in the previous section, and other statements. (These other statements are often called executable statements. In C++, however, data declarations can also involve executable code.) This last category is a large one, and I break it down somewhat in the next section.
What Can I Do with a Statement?After declaring variables, you can use statements to perform a number of actions. These actions usually manipulate or use the variables in some way. In C++, you can mix declarations and other statements in any order. (You cant do that in C.) But it usually makes sense to declare variables first, because in both C and C++ you must explicitly declare a variable before using it. Aside from control structures and functions, which Ill cover later, all the actions you can perform boil down to these three:
The next three sections discuss how you do each of these actions in C++. Assigning ValuesAssignment statements in C++ look similar to those in other languages, especially Basic. The main difference from Basic is that C++ statements are terminated by semicolons. Before assigning data to a variable, make sure you declare the variable: int amount, a, b, c; To assign a value to one of these variables, place the variable name on the left side of an equal sign (=). On the right side, place a variable, a constant (such as 1 or -240), or a compound expression (such as 2 * c). a = 1; amount = -240; b = 2 * c; amount = a + 10 * b * -1; Note that in C++, the asterisk (*) represents multiplication.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |